package com.cc.controller;

import com.cc.entity.EbProduct;
import com.cc.service.IEbProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@Controller
@RequestMapping("/product")
public class EbProductController  {
    @Autowired
    private IEbProductService iEbProductService;

    @RequestMapping("/detail")
    public String detail(Model model,int epid){
        EbProduct ebProduct=iEbProductService.detail(epid);
        model.addAttribute("ebProduct",ebProduct);
        return "product-view";

    }

    @RequestMapping("/addCar")
    public String addCar(HttpServletRequest request, int epid){
       EbProduct productDetail=iEbProductService.detail(epid);
       HttpSession session=request.getSession();
        Set<EbProduct> productSet=(Set<EbProduct>)session.getAttribute("productSet");
        if (productSet==null){
            productSet=new HashSet<>(5);
        }
        for (EbProduct product:productSet){
            if(product.getEpid()==productDetail.getEpid()){
                productDetail.setCount(product.getCount()+1);
                productSet.remove(product);
            }
        }
        productSet.add(productDetail);
        session.setAttribute("productSet",productSet);
        return "shopping";
    }

    @RequestMapping("/list")
    public String a(Model model, @RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "pageSize",defaultValue = "3") int pageSize){
        List<EbProduct> ebProses=iEbProductService.findByPage(page,pageSize);
        model.addAttribute("ebProses",ebProses);
        model.addAttribute("curPage",page);
        int totalnum=iEbProductService.total();//总记录条数
        double no=(double)totalnum/pageSize;//总页数
        int num=(int)Math.ceil(no);//向上取整总页数
        model.addAttribute("totalpages",num);
        return "manage/product";
    }
    @RequestMapping("/delete/{epid}")
    public String b(@PathVariable("epid") int id){
        EbProduct ebProduct=iEbProductService.selectByCondition(id);
        File f=new File("F:\\IdeaProjects\\idea_prj10.9\\easybuy_PSY\\src\\main\\webapp\\WEB-INF\\images\\product\\"+ebProduct.getEpfilename());
        f.exists();
        int result=iEbProductService.delete(id);
        if(result==0){
            return"manage/paramError";
        }
        System.out.println("1");
        return "manage/manage-result";
    }
    @RequestMapping("/edit")
    public String c(Model model,int epid){
        EbProduct ebProduct=iEbProductService.selectByCondition(epid);
        model.addAttribute("ebProduct",ebProduct);
        return "manage/product-modify";
    }
    @RequestMapping("/editSubmit")
    public String d(EbProduct ebProduct){
         int result=iEbProductService.updateById(ebProduct);
         if (result<=0){
            return "manage/paramError";
        }
        return "manage/manage-result";
    }
    @RequestMapping("/to_save")
    public String e() {
        return "manage/product-add";

    }
    @RequestMapping("/save_product")
    public String f(EbProduct ebProduct, MultipartFile pictureFile) throws IOException {
        if((ebProduct.getEpname()==null||ebProduct.getEpname().trim().length()==0)&&
                (ebProduct.getEpdescription()==null||ebProduct.getEpdescription().trim().length()==0)&&
                (ebProduct.getEpprice()==0)&&
                (ebProduct.getEpstock()==0)){
            return "manage/paramError";
        }
        String pictureFile_name=pictureFile.getOriginalFilename();
        String newFileName= UUID.randomUUID().toString()+pictureFile_name.substring(pictureFile_name.lastIndexOf("."));
        ebProduct.setEpfilename(newFileName);
        File uploadPic=new java.io.File("F:\\IdeaProjects\\idea_prj10.9\\easybuy_PSY\\src\\main\\webapp\\WEB-INF\\images\\product\\"+newFileName);
        try {
        if(!uploadPic.exists()){
            uploadPic.mkdirs();
        }
        pictureFile.transferTo(uploadPic);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int result=iEbProductService.save(ebProduct);
        if(result<=0){
            return"manage/paramError";
        }
        return "manage/manage-result";
    }
}
